/* (PARI) R. J. Cano, Sep 18 2015 */

/*
 * Both counts the same, clumsy shows a safe method for deducing such counters.
 * By extension it should be useful for the 3D cases! :-)
 * 
 * In such case you need to define for example a vector of matrices, giving this the third coordinate.
 * The it must be followed a similar procedure like the shown below with "clumsy". After we ensure it
 * is correct for any case, then by analysis of the loops we deduce the direct formulas (if possible).
*/
clumsyRemii(x,y)={my(A=matrix(x+2,y+2,i,j,1),z=0);for(p=2,x+1,for(q=2,y+1,A[p,q]*=0));for(p=1,x+2,for(q=1,y+2,z+=A[p,q]));z}

cleverRemii(x,y)=2*(x+y+2); /* Based upon clumsy and simplified using properties of sums. */

s(x,y)=cleverRemii(x,y);

/* Calculates: How many rectangles need a*n squares in order to be fully
 * surrounded by a layer; Notice here the default value for "a" is 1 */
F(n,a=1)={my(c=0);forvec(w=vector(2,k,[1,a*n]),c+=!(n*a-s(w[1],w[2])));c} /* a*n as the upper bound here is JUST an arbitrary exageration. */

/* Sample output (Running on a 32Bits machine with 1.6Ghz and 1Gb Ram, Linux):

? vector(100,j,F(j))
time = 1,666 ms.
%13 = [0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0, 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47]

? vector(100,j,F(j,2))
time = 6,737 ms.
%14 = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97]
? 

*/